/**
* Jquery模拟弹出窗口组件
*
* 本程序主要作用弹出窗口并居中
* 
* @category   Javascript
* @package    Javascript
* @copyright  Copyright (c) ink工作室 (http://www.inkphp.com)
* @author     张旭刚
* @version    v1.0 beta
*/
$.win = {
    open:function(title, url){
        var html = this.getHtml();
        var o = this;
        $('body').append(html);
        $('#titleWin').text(title);
        $('#closeWin').click(function(){
            o.close();
        });
        this.load(url);
    },
    close:function(){
        var obj = $('#closeWin');
        var o = obj.parent().parent();
        o.remove();
        $('#cover').remove();
    },
    getHtml:function(){
        var html = '<div id="cover" class="cover"></div>';
        html += '<div id="openWin" class="openWin">';
        html += '   <h1 class="tWin"><span class="titleWin" id="titleWin"></span><em class="closeWin" id="closeWin"></em></h1>'
        html += '   <div id="winBody" class="winBody"></div>';
        html +='</div>';
        return html;
    },
    load:function(url){
        var o = this;
        $.get(url,{},function(t){
            $('#winBody').html(t);
            $('#cover').show('');
            $('#openWin').fadeIn('slow');
            o.setCenter();
        },'html');
    },
    setCenter:function(type){
        if(type == null){
            type = 'iframe';
        }
//        alert(type);
        var sizes = this.getSize();
        var width = sizes.width;
        var height = sizes.height;
        if(type == 'iframe'){
            var iframe = parent.$('#I2');
            var top = Math.round((iframe.attr('height') - height)/2);
            var left = Math.round((iframe.attr('scrollWidth') - width)/2);
        }else{
            var top = Math.round(($('body').attr('scrollHeight') - height)/2);
            var left = Math.round(($('body').attr('scrollWidth') - width)/2);
        }
        var obj = $('#openWin');
        if(top < 1){top = 1;}
        if(left < 1){left = 1;}
        obj.css('top', top);
        obj.css('left', left);
    },
    setPosition:function(iframe){
        var coverObj = $('#cover');
        var winObj = $('#openWin');
        var coverTop = coverObj.css('top');
        var winTop = winObj.css('top');
        var bodyTop = iframe.attr('scrollTop');
        coverObj.css('top', (coverTop+bodyTop));
        winTop.css('top', (winTop+bodyTop));
    },
    getSize:function(){
        var w = $('#openWin').attr('offsetWidth');
        var h = $('#openWin').attr('offsetHeight');
        var data = {'width':w,'height':h};
        return data;
    }
}